#!/usr/bin/env bash

main() {
    local SCRIPT_DIR="$(dirname "$(realpath "$0")")"
    source "$SCRIPT_DIR/lib/utils.sh"
    source "$SCRIPT_DIR/lib/info.sh"
    source "$SCRIPT_DIR/lib/sub_commands.sh"
    source "$SCRIPT_DIR/lib/container.sh"

    rm -f "$SCRIPT_DIR/.src/build.log"

    local ARGV=("$@") TEMP
    if ! TEMP="$(getopt -o "r:cCphnlsdb:" -l "revision:,clean,distclean,no-prepare-source,no-config,help,native-build,local-container,container-shell,debug,dtb,long-version,backend:,no-submodule-check,dirty,no-build,no-container-update" -n "$0" -- "$@")"
    then
        return
    fi
    eval set -- "$TEMP"

    local PKG_REVISION="1"
    local NO_PREPARE_SOURCE="false"
    local NO_CONFIG="false"
    local NO_BUILD="false"
    local NO_CONTAINER_UPDATE="false"
    local CLEAN_LEVEL=
    local PATCH_PAUSE="false"
    local NATIVE_BUILD="false"
    local CONTAINER_REGISTRY="ghcr.io/radxa-repo/"
    local CONTAINER_SHELL="false"
    local DEBUG_BUILD="false"
    local DTB_ONLY="false"
    local LONG_VERSION="false"
    local CONTAINER_BACKEND="docker"
    local NO_SUBMODULE_CHECK="false"

    if [[ -f "$SCRIPT_DIR/.bsp-config" ]]
    then
        source "$SCRIPT_DIR/.bsp-config"
    fi

    while true
    do
        TEMP="$1"
        shift
        case "$TEMP" in
            -r|--revision)
                PKG_REVISION="$1"
                shift
                ;;
            --no-prepare-source)
                NO_PREPARE_SOURCE="true"
                ;;
            --no-config)
                NO_CONFIG="true"
                ;;
            --no-container-update)
                NO_CONTAINER_UPDATE="true"
                ;;
            --dirty)
                NO_PREPARE_SOURCE="true"
                NO_CONFIG="true"
                NO_CONTAINER_UPDATE="true"
                ;;
            --no-build)
                NO_BUILD="true"
                ;;
            -p)
                PATCH_PAUSE="true"
                ;;
            -c|--clean)
                CLEAN_LEVEL="clean"
                ;;
            -C|--distclean)
                CLEAN_LEVEL="distclean"
                ;;
            -n|--native-build)
                NATIVE_BUILD="true"
                ;;
            -l|--local-container)
                CONTAINER_REGISTRY=
                ;;
            -s|--container-shell)
                CONTAINER_SHELL="true"
                ;;
            -d|--debug)
                DEBUG_BUILD="true"
                ;;
            --dtb)
                DTB_ONLY="true"
                ;;
            -b|--backend)
                CONTAINER_BACKEND="$1"
                shift
                ;;
            --long-version)
                LONG_VERSION="true"
                ;;
            --no-submodule-check)
                NO_SUBMODULE_CHECK="true"
                ;;
            -h|--help)
                usage
                return
                ;;
            --)
                break
                ;;
            *)
                error $EXIT_UNKNOWN_OPTION "$TEMP"
                ;;
        esac
    done

    if ! $NO_SUBMODULE_CHECK && [[ -z "$(git submodule status)" ]]
    then
        error $EXIT_NO_SUBMODULE
    fi

    TEMP="${1:-}"
    shift || true

    case "$TEMP" in
        install|export|import|json)
            "_$TEMP" "$@"
            return
            ;;
        "")
            usage
            return
            ;;
        *)
            if ! $NATIVE_BUILD
            then
                container_exec; return $?
            fi
            ;;
    esac

    case "$TEMP" in
        linux|u-boot)
            build "$TEMP" "$@"
            ;;
        *)
            local components=( $(get_supported_component) )
            echo "'$TEMP' is not part of the supported components: ${components[*]}"
            echo "Assume it is a product."

            for c in "${components[@]}"
            do
                local profiles=( $(query_supported_profiles "$c" "$TEMP") )
                if (( ${#profiles[@]} ))
                then
                    echo "'$TEMP' is supported by the following $c profiles: ${profiles[*]}"
                else
                    echo "'$TEMP' is not supported by $c."
                fi
            done

            read -p "Do you want to build supported components with the first profile listed? [Y/n] "
            case "$(awk '{print tolower($0)}' <<< "$REPLY")" in
                ""|y|ye|yes)
                    for c in "${components[@]}"
                    do
                        local profiles=( $(query_supported_profiles "$c" "$TEMP") )
                        if (( ${#profiles[@]} ))
                        then
                            build "$c" "${profiles[0]}" "$TEMP"
                        fi
                    done
                    ;;
                *)
                    echo "Operation cancelled."
                    ;;
            esac
            ;;
    esac
}

main "$@"
